using System;
using System.Collections.Generic;
using System.Linq;
using Voodoo.Sauce.Internal.Analytics;
using Voodoo.Tune.Core;

namespace Voodoo.Sauce.Internal.VoodooTune
{
	internal class VoodooTuneAbTestsTracker
	{
		private const string TAG = "VoodooTuneAbTestsTracker";

		private readonly List<string> _abTests;

		private readonly List<string> _cohorts;

		private readonly string _version;

		internal VoodooTuneAbTestsTracker()
		{
			_abTests = VoodooTuneManager.GetAbTestUuidsAsList();
			_cohorts = VoodooTuneManager.GetAbTestCohortUuidsAsList();
			_version = VoodooTuneManager.GetAbTestVersionUuid();
		}

		public void TrackAbTestModifications(Voodoo.Tune.Core.VoodooConfig config)
		{
			TrackAbTestModifications(config.AbTestIdsToList, config.CohortIdsToList, config.VersionNumber);
		}

		private void TrackAbTestModifications(List<string> abTestUuids, List<string> abTestCohortUuids, string abTestVersionUuid)
		{
			if (abTestUuids.SequenceEqual(_abTests) && abTestCohortUuids.SequenceEqual(_cohorts))
			{
				return;
			}
			VoodooLog.LogDebug(Module.VOODOO_TUNE, "VoodooTuneAbTestsTracker", "Tracking A/B Tests modifications");
			var (list3, list4) = GetAssignmentsAndExits(_abTests, _cohorts, abTestUuids, abTestCohortUuids, _version, abTestVersionUuid);
			foreach (VoodooTuneAbTestAnalyticsInfo item in list3)
			{
				if (!string.IsNullOrEmpty(item.AbTestUuid) && !string.IsNullOrEmpty(item.AbTestCohortUuid))
				{
					string.IsNullOrEmpty(item.AbTestVersionUuid);
				}
			}
			foreach (VoodooTuneAbTestAnalyticsInfo item2 in list4)
			{
				if (!string.IsNullOrEmpty(item2.AbTestUuid) && !string.IsNullOrEmpty(item2.AbTestCohortUuid))
				{
					string.IsNullOrEmpty(item2.AbTestVersionUuid);
				}
			}
		}

		internal static Tuple<List<VoodooTuneAbTestAnalyticsInfo>, List<VoodooTuneAbTestAnalyticsInfo>> GetAssignmentsAndExits(List<string> oldAbTestUuids, List<string> oldAbTestCohortUuids, List<string> abTestUuids, List<string> abTestCohortUuids, string oldAbTestVersionUuid, string abTestVersionUuid)
		{
			try
			{
				List<VoodooTuneAbTestAnalyticsInfo> abTestsAnalyticsInfo = GetAbTestsAnalyticsInfo(oldAbTestUuids, oldAbTestCohortUuids, oldAbTestVersionUuid);
				List<VoodooTuneAbTestAnalyticsInfo> abTestsAnalyticsInfo2 = GetAbTestsAnalyticsInfo(abTestUuids, abTestCohortUuids, abTestVersionUuid);
				List<VoodooTuneAbTestAnalyticsInfo> item = abTestsAnalyticsInfo2.Except(abTestsAnalyticsInfo).ToList();
				List<VoodooTuneAbTestAnalyticsInfo> item2 = abTestsAnalyticsInfo.Except(abTestsAnalyticsInfo2).ToList();
				return new Tuple<List<VoodooTuneAbTestAnalyticsInfo>, List<VoodooTuneAbTestAnalyticsInfo>>(item, item2);
			}
			catch
			{
				VoodooLog.LogWarning(Module.VOODOO_TUNE, "VoodooTuneAbTestsTracker", $"AB tests are misconfigured: get {oldAbTestUuids.Count} saved AB test UUIDs, {oldAbTestCohortUuids.Count} saved cohort UUIDs and get {abTestUuids.Count} AB test UUIDs, {abTestCohortUuids.Count} cohort UUIDs");
				return new Tuple<List<VoodooTuneAbTestAnalyticsInfo>, List<VoodooTuneAbTestAnalyticsInfo>>(new List<VoodooTuneAbTestAnalyticsInfo>(), new List<VoodooTuneAbTestAnalyticsInfo>());
			}
		}

		private static List<VoodooTuneAbTestAnalyticsInfo> GetAbTestsAnalyticsInfo(IReadOnlyList<string> abTests, IReadOnlyList<string> cohorts, string version)
		{
			List<VoodooTuneAbTestAnalyticsInfo> list = new List<VoodooTuneAbTestAnalyticsInfo>();
			if (abTests == null || cohorts == null)
			{
				return list;
			}
			if (abTests.Count != cohorts.Count)
			{
				throw new Exception($"AB tests are misconfigured: get {abTests.Count} AB test UUIDs, {cohorts.Count} cohort UUIDs");
			}
			for (int i = 0; i < abTests.Count; i++)
			{
				VoodooTuneAbTestAnalyticsInfo item = new VoodooTuneAbTestAnalyticsInfo(abTests[i], cohorts[i], version);
				list.Add(item);
			}
			return list;
		}
	}
}
